<template>
    <el-dialog v-model="dialogVisible"
    :title="requestParam.id==null||requestParam.id==''?'添加会员类型':'修改会员类型'" 
    width="30%">
        <el-form :model="requestParam" ref="form" label-position="top" label-width="80px">
            <el-form-item label="类型名称" prop="name">
                <el-input size="large" v-model="requestParam.name" placeholder="请输入类型名称"></el-input>
            </el-form-item>

            <el-form-item label="类型描述" prop="description">
                <el-input size="large" v-model="requestParam.description" placeholder="请输入分组描述"></el-input>
            </el-form-item>
            <el-form-item label="类型编码" prop="code">
                <el-input size="large" v-model="requestParam.code" placeholder="请输入类型编码"></el-input>
            </el-form-item>

        </el-form>
        <span class="dialog-footer">
            <el-button @click="close">取 消</el-button>
            <el-button type="primary" @click="submit">确 定</el-button>
        </span>
        <div style="height: 20px;"></div>
    </el-dialog>
</template>

<script lang="ts" setup>
import { ref, defineExpose, defineEmits } from 'vue';
import $kt from '@/vueKantboot';
import { ElMessage } from 'element-plus';


let dialogVisible = ref(false);

let requestParam = ref({
    id: '',
    name: '',
    description: '',
    code: '',
    "delete": false,
});

let close = () => {
    dialogVisible.value = false;
}

let open = (item:any) => {
    if(item!=null) {
        requestParam.value = JSON.parse(JSON.stringify(item));
    }else {
        requestParam.value.id = '';
        requestParam.value.name = '';
        requestParam.value.description = '';
        requestParam.value.code = '';
    }
    dialogVisible.value = true;
}

const emits = defineEmits(["flush"]);
let submit = () => {

    if(requestParam.value.name == '') {
        ElMessage({
            message: '请输入分类型名称',
            type: 'error',
        });
        return false;
    }

    if(requestParam.value.description == '') {
        ElMessage({
            message: '请输入类型描述',
            type: 'error',
        });
        return false;
    }



    if(requestParam.value.code == '') {
        ElMessage({
            message: '请输入类型编码',
            type: 'error',
        });
        return false;
    }
    
    $kt.request.request({
        url: '/adminManage/business/ovoShop/goodsType/save',
        method: 'post',
        data: requestParam.value
    }).then((res: any) => {
        if(res.data.state != 2000) {
            ElMessage({
                message: res.data.errMsg,
                type: 'error',
            });
            return false;
        }
        ElMessage({
            message: res.data.msg,
            type: 'success',
        });

        // 重置requestParam
        requestParam.value.name = '';
        requestParam.value.description = '';
        requestParam.value.code = '';
        

        close();
        emits("flush");
    });
}

defineExpose({
    open,
    close
});
</script>

<style lang="scss" scoped>
.dialog-footer {
    float: right;
}
</style>